<?php
/**
* ExceptionHandler.php
*
* Logging functions.
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("config.php");


class ExceptionHandler {  

    /**
     * Class c'tor
     */
    function ExceptionHandler( )
    {
        //Register function for exception handling
        set_exception_handler(array("ExceptionHandler", "handleException"));
        trigger_error("LEAVE ExceptionHandler::ExceptionHandler() ", E_USER_NOTICE);
    }
    
    public static function printException(Exception $e)
    {
	    $options = &PEAR::getStaticProperty('CONFIG_INI','DEBUG');

        $exception  = "";
        $exception .= "" .date("Y-m-d H:i:s"). "";
        $exception .= "\t" . get_class($e) . "";
        $exception .= "\t" . $e->getCode() . "";
        $exception .= "\t" . $e->getMessage() . "";
        $exception .= "\t" . $e->getFile() . "";
        $exception .= "\t" . $e->getLine() . "";
        $exception .= "\t" . $e->getTraceAsString() . "";
        $exception .= "\n";

        if ( $options["debug"] ) 
        {
            echo $exception;
        } 
        else
        {
            ExceptionHandler::errorLog($exception, $options["log_file"]  );
        }
        
    }
  
    public static function handleException(Exception $e)
    {
         self::printException($e);
    }


    /**
     * Custom error log
     */
    static function errorLog($err,$file)
    {   
        if (is_writable($file) || !file_exists($file) ) {

            if (!$handle = fopen($file, 'a')) {
                trigger_error("UserErrorHandler::errorLog(): Could not create file.", E_USER_ERROR);
                return;
            }  


            if (fwrite($handle, $err) === FALSE) {
                trigger_error("UserErrorHandler::errorLog(): Could not write to file.", E_USER_ERROR);
                return;
            }
            fclose($handle);
        }
        else
        {
            trigger_error("UserErrorHandler::errorLog(): File read only.", E_USER_ERROR);
        }
        
    }  
}
$exception = new ExceptionHandler;

?>
